package com.hengtongxing.hejiayun_employee.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.hengtongxing.hejiayun_employee.R;
import com.hengtongxing.hejiayun_employee.widget.RoundedCornersTransform;

/**
 * Time: 2019/7/30
 * Author: hwc
 * Description:
 */
public class GlideUtils {

    public static void loadImage(Context context, ImageView imageView, String imgUrl) {
        RequestOptions options = new RequestOptions()//
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//
                .placeholder(R.color.bgColor_default)//
                .error(R.color.bgColor_default);
        Glide.with(context)//
                .asDrawable()//
                .apply(options)//
                .thumbnail(0.3f).load(imgUrl)//
                .into(imageView);
    }

    /**
     * 展示带圆角的图片
     *
     * @param roundingRadius 圆角px;
     */
    public static void loadImage_Conner(Context context, ImageView imageView, String imgUr, int roundingRadius) {

        RequestOptions options = RequestOptions.bitmapTransform(new RoundedCorners(roundingRadius))//
                .skipMemoryCache(false)//
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//
                .placeholder(R.color.bgColor_default)//
                .error(R.color.bgColor_default);

        Glide.with(context)//
                .asDrawable()//
                .apply(options)//
                .load(imgUr)//
                .into(imageView);
    }

    /**
     * 展示部分带圆角的图片
     *
     * @param roundingRadius 圆角px;
     */
    public static void loadImage_TopConners(Context context, ImageView imageView, String imgUr, int roundingRadius) {

        RoundedCornersTransform transform = new RoundedCornersTransform(context, roundingRadius);
        transform.setNeedCorner(true, true, false, false);

        RequestOptions options = RequestOptions.bitmapTransform(transform)//
                .skipMemoryCache(false)//
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//
                .placeholder(R.color.bgColor_default)//
                .error(R.color.bgColor_default);

        Glide.with(context)//
                .asDrawable()//
                .apply(options)//
                .load(imgUr)//
                .into(imageView);
    }

    /**
     * 展示带圆的图片
     */
    public static void loadImage_Circle(Context context, ImageView imageView, String imgUrl) {

        RequestOptions options = new RequestOptions()//
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//
                .placeholder(R.color.bgColor_default)//
                .error(R.color.bgColor_default);

        Glide.with(context)//
                .asDrawable()//
                .apply(options)//
                .load(imgUrl)//
                .into(imageView);
    }

    /**
     * 加载头像
     */
    public static void loadImage_header(Context context, ImageView imageView, String imgUrl) {
        RequestOptions options = new RequestOptions()//
                .circleCrop()//
                .skipMemoryCache(false)//
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//
                .placeholder(R.mipmap.ic_launcher)//
                .error(R.mipmap.ic_launcher);

        Glide.with(context)//
                .asDrawable()//
                .apply(options)//
                .load(imgUrl)//
                .into(imageView);
    }

    /**
     * 加载头像
     */
    public static void loadImage_header_cache(Context context, ImageView imageView, String imgUrl) {

        RequestOptions options = new RequestOptions()//
                .circleCrop()//
                .skipMemoryCache(false)//
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//
                .placeholder(R.mipmap.ic_launcher)//
                .error(R.mipmap.ic_launcher);

        Glide.with(context)//
                .asDrawable()//
                .apply(options)//
                .load(imgUrl)//
                .into(imageView);
    }

    public static void downloadBitmap(Context context, ImageView imageView, String imgUrl, @NonNull BitmapLoadListener listener) {

        RequestOptions options = new RequestOptions()//
                .circleCrop()//
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//
                .placeholder(R.mipmap.ic_launcher)//
                .error(R.mipmap.ic_launcher);

        Glide.with(context)//
                .asBitmap()//
                .load(imgUrl)//
                .apply(options)//
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        listener.getBitmap(resource);
                        return false;
                    }
                })//
                .into(imageView);

    }


    public interface BitmapLoadListener {
        void getBitmap(Bitmap bitmap);
    }
}
